<?php
namespace app\api\controller;

use think\Controller;
use think\Db;
use app\common\model\Kouling as KoulingModel;
use app\common\model\UsersKouling as UsersKoulingModel;

class Kouling extends Controller
{
    protected $kouling_model;
    protected $users_kouling_model;

    protected function  _initialize(){
        parent::_initialize();
        $this->users_kouling_model = new UsersKoulingModel();
        $this->kouling_model = new KoulingModel();
    }

    public function get_kouling(){
        $result = $this->kouling_model->query('SELECT t1.* FROM `think_kouling` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `think_kouling`)-(SELECT MIN(id) FROM `think_kouling`))+(SELECT MIN(id) FROM `think_kouling`)) AS id) AS t2   
        WHERE t1.id >= t2.id   AND t1.status = 1
        ORDER BY t1.id LIMIT 1');
        return json($result[0]);
    }

    public function get_publish_kouling(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $user_kouling = $this->users_kouling_model->where('id',$data['id'])->find();
            $receive = Db::name('receive')->where('k_id',$data['id'])->select();
            // dump($user_kouling);
            return json(['user_kouling'=>$user_kouling,'receive'=>$receive]);
        }
    }
}